import { reactive, readonly } from 'vue'
//TODO 从库中加载
import { getConfig } from '@/api/cms/system/index'

export interface BaseSettings {
  filesExtensionBlacklist: string
  uploadsExtensionBlacklist: string
}

export interface UploadSettings {
  imageLimits: string
  imageInputAccept: string
  videoInputAccept: string
  audioInputAccept: string
  libraryInputAccept: string
  attachInputAccept: string
  imageLimitByte: number
  videoLimitByte: number
  audioLimitByte: number
  libraryLimitByte: number
  attachLimitByte: number
}

export interface SecuritySettings {
  passwordMinLength: number
  passwordMaxLength: number
  passwordStrength: number
  passwordPattern: string
  weakPasswordProcessMode: number
  weakPasswordCheck: boolean
}

export interface Config {
  base: BaseSettings
  upload: UploadSettings
  security: SecuritySettings
  register: RegisterSettings
  other: OtherSettings
}
export interface OtherSettings {
  sensitiveWordProcessModel: number
}
export interface RegisterSettings {
  enabled: Boolean
}

const state = reactive<Config>({
  base: {
    filesExtensionBlacklist: 'exe,com,bat,jsp,jspx,asp,aspx,php',
    uploadsExtensionBlacklist: 'exe,com,bat,jsp,jspx,asp,aspx,php,html,htm,xhtml,xml,shtml,shtm'
  },
  upload: {
    imageLimits: 'jpg,jpeg,png,gif',
    imageInputAccept: '.jpg,.jpeg,.png,.gif',
    videoInputAccept: '.mp4,.m3u8',
    audioInputAccept: '.mp3,.ogg,.wav',
    libraryInputAccept: '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx',
    attachInputAccept:
      '.zip,.7z,.gz,.bz2,.iso,.rar,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.jpg,.jpeg,.png,.gif,.mp4,.m3u8,.mp3,.ogg',
    imageLimitByte: 0,
    videoLimitByte: 0,
    audioLimitByte: 0,
    libraryLimitByte: 0,
    attachLimitByte: 0
  },
  security: {
    passwordMinLength: 0,
    passwordMaxLength: 64,
    passwordStrength: 0,
    passwordPattern: '.*',
    weakPasswordProcessMode: 2,
    weakPasswordCheck: false
  },
  register: {
    enabled: false
  },
  other: {
    sensitiveWordProcessModel: 1
  }
})
export const baseSettings = readonly(state).base
export const uploadSettings = readonly(state).upload
export const securitySettings = readonly(state).security
export const otherSettings = readonly(state).other
export const initConfig = async () => {
  const config = await getConfig()
  Object.assign(state.base, config)
  Object.assign(state.upload, config.uploadConfig)
  Object.assign(state.register, config.registerConfig)
  Object.assign(state.security, config.securityConfig)
  Object.assign(state.other, config.otherConfig)
}
// 初始化
initConfig()
